package com.sun.juc.thread;

import org.junit.Test;

import java.util.concurrent.TimeUnit;

public class ThreadDemo {
    public static void main(String[] args) {
        System.out.println(Thread.currentThread().getName() + "正在执行");
        test2();
    }

    @Test
    public void test1() throws InterruptedException {
        asd asd = new asd();
        new Thread(() -> {

            try {
                Thread.sleep(1000);
                System.out.println(asd.i);

                asd.i++;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(() -> {
            try {
                Thread.sleep(1000);
                System.out.println(asd.i);

                asd.i++;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        Thread.sleep(3000);
        System.out.println(asd.i);
    }

    class asd {
        public int i = 0;
    }

    public static void test2() {
        Thread thread = new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "开始");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "结束");
        });
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Thread thread2 = new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "开始");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "结束");
        });
        thread2.start();
        System.out.println(Thread.currentThread().getName() + "结束");
    }
}

